package com.service.sys;

import com.common.ErrorCodeEnum;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.sys.SysRoleDeptMapper;
import com.github.pagehelper.PageHelper;
import com.model.pojo.sys.SysDept;
import com.model.pojo.sys.SysRoleDept;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: zql
 * @Description: 角色部门关联表
 * @Data: 16:48 2021/5/11
 */
@Slf4j
@Service
public class SysRoleDeptService {
    @Resource
    SysRoleDeptMapper sysRoleDeptMapper;

    @Resource
    SysDeptService sysDeptService;

    /**
     * @Author: zql
     * @Description: 获取部门列表
     * @param sysRoleId 角色id
     * @Data: 15:16 2021/7/5
     */
    public List<SysDept> getDeptListByRoleId(Long sysRoleId){
        PageHelper.clearPage();
        List<SysRoleDept> sysRoleDeptList= sysRoleDeptMapper.selectBySysRoleId(sysRoleId);
        List<SysDept> sysDeptList=new ArrayList<>();
        sysRoleDeptList.forEach(sysRoleDept -> {
            try {
                SysDept sysDept = sysDeptService.get(sysRoleDept.getSysDeptId(),null);
                if (sysDept.getIsDelete().equals(DataStatus.OK.getCode())) {
                    sysDeptList.add(sysDept);
                }else{
                    log.error("无法根据角色获取部门，角色绑定的部门状态:{} roleId:{},deptId:{}",DataStatus.info(sysDept.getIsDelete()), sysRoleId, sysDept.getSysDeptId());
                }
            }catch (BusinessException e){
                log.error("无法根据角色获取部门，角色绑定了不存在的部门roleId:{},deptId:{},code:{},msg:{}", sysRoleId, sysRoleDept.getSysDeptId(),
                        e.getErrorCodeEnum().getErrorCode(), e.getErrorCodeEnum().getErrorMsg());
            }
        });
        return sysDeptList;
    }

    /**
     * @Author: zql
     * @Description: 校验部门列表是否存在
     * @param deptIds 部门id列表
     * @Data: 10:06 2021/5/12
     */
    public void verifySysDeptId(Set<Long> deptIds){
        if(StringUtils.isNull(deptIds)){
            throw new BusinessException(ErrorCodeEnum.DEPT_IDS_NOT_FOUND);
        }
        for (Long deptId:deptIds) {
            if(StringUtils.isNull(deptId)){
                throw new BusinessException(ErrorCodeEnum.PARAM_FAILED);
            }
            if (!sysDeptService.has(deptId)) {
                if(sysDeptService.has(deptId,DataStatus.DISABLE.getCode())){
                    SysDept sysDept=  sysDeptService.get(deptId,null);
                    throw new BusinessException(ErrorCodeEnum.DEPT_STOP.setParam(deptId.toString(),sysDept.getDeptName()));
                }
                if(sysDeptService.has(deptId,DataStatus.DELETED.getCode())){
                    SysDept sysDept=  sysDeptService.get(deptId,null);
                    throw new BusinessException(ErrorCodeEnum.DEPT_DELETE.setParam(deptId.toString(),sysDept.getDeptName()));
                }

                throw new BusinessException(ErrorCodeEnum.DEPT_NOT_FOUND);
            }
        }
    }

    /**
     * @Author: zql
     * @Description: 绑定角色部门列表
     * @param roleId 角色id
     * @param deptIds 部门id列表
     * @Data: 15:15 2021/7/5
     */
    public void binding(Long roleId, Set<Long> deptIds) {
        if (deptIds.size() == 0){
            return;
        }
        String name=SecurityUtils.getUsername();
        SysRoleDept sysRoleDept = new SysRoleDept();
        for(Long deptId:deptIds){
            sysRoleDept.setSysRoleDeptId(null);
            sysRoleDept.setSysRoleId(roleId);
            sysRoleDept.setSysDeptId(deptId);
            sysRoleDept.setUpdateTime(null);
            sysRoleDept.setCreateTime(null);
            sysRoleDept.setModifier(name);
            sysRoleDept.setCreator(name);
            sysRoleDept.setIsDelete(DataStatus.OK.getCode());
            sysRoleDeptMapper.insertSelective(sysRoleDept);
        }
    }

    /**
     * @Author: zql
     * @Description: 删除与角色绑定的部门
     * @param roleId 角色id
     * @Data: 15:15 2021/7/5
     */
    public void uBindList(Long roleId){
        sysRoleDeptMapper.deleteBySysRoleId(roleId);
    }
}
